<?php
class B2b_Models_Dao_Pdo_Mysql_Gallery extends Tomato_Model_Dao
    implements B2b_Models_Interface_Gallery
{
    public function convert($entity)
    {
        return new B2b_Models_Gallery($entity);
    }

    public function add($gallery){
        return $this->_conn->insert($this->_prefix . 'enterprise_gallery',
                            array(
                                'set_id'        => $gallery->set_id,
                                'enterprise_id' => $gallery->enterprise_id,
                                'num_image'     => $gallery->num_image,
                            ));
    }

    public function getSetInfo($id, $offset = null, $count = null){
        $select = $this->_conn
                        ->select()
                        ->from(array('g' => $this->_prefix . 'enterprise_gallery'), array('enterprise_id', 'num_image'))
                        ->joinInner(array('s' => $this->_prefix . 'multimedia_set'), "g.set_id = s.set_id")
                        ->where('g.enterprise_id = '. $id )
                        ->order('g.set_id DESC')
                        ;
                        
        if(null != $count){
            $select->limit($count, $offset);
        }
        $rs = $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);
    }

    public function count($id){
        $select = $this->_conn
                        ->select()
                        ->from(array('g' => $this->_prefix . 'enterprise_gallery'), array('num_gallery' => 'COUNT(*)'))
                        ->joinInner(array('s' => $this->_prefix . 'multimedia_set'), "g.set_id = s.set_id", array())
                        ->where('g.enterprise_id = ?', $id);

        return $select->query()->fetch()->num_gallery;
    }

    public function updateSet($set)
    {
        return $this->_conn->update($this->_prefix . 'multimedia_set',
                                    array(
                                        'image_square'    => $set->image_square,
                                        'image_thumbnail' => $set->image_thumbnail,
                                        'image_small'     => $set->image_small,
                                        'image_crop'      => $set->image_crop,
                                        'image_medium'    => $set->image_medium,
                                        'image_large'     => $set->image_large,
                                    ),
                                    array(
                                        'set_id = ?' => $set->set_id,
                                    ));
    }

    public function updateSetName($set){
        return $this->_conn->update($this->_prefix . 'multimedia_set',
                                    array(
                                        'title'    => $set->title,
                                        'slug'    => $set->slug,
                                    ),
                                    array(
                                        'set_id = ?' => $set->set_id,
                                    ));
    }

    public function updateNImage($id, $num, $operator = '+'){
        $sql = "UPDATE `{$this->_prefix}enterprise_gallery` SET `num_image` = `num_image`{$operator}{$num} WHERE `set_id` = {$id}";
        return $this->_conn->query($sql);
    }

    public function removeFromSet($file_id){
        $where = array('file_id = ?' => $file_id,);
        return $this->_conn->delete($this->_prefix . 'multimedia_file_set_assoc', $where);
    }

    public function deleteAlbum($id){
        return $this->_conn->delete($this->_prefix . 'enterprise_gallery',
                                    array(
                                        'set_id = ?' => $id,
                                    ));
    }
    
	public function mark($file_id)
	{
		return $this->_conn->update($this->_prefix . 'multimedia_file_set_assoc',
                                    array(
                                        'mark'    => 1,
                                    ),
                                    array(
                                        'file_id = ?' => $file_id,
                                    ));
	}
	
	public function unmark($file_id)
	{
		return $this->_conn->update($this->_prefix . 'multimedia_file_set_assoc',
                                    array(
                                        'mark'    => 0,
                                    ),
                                    array(
                                        'file_id = ?' => $file_id,
                                    ));
	}
	
	public function checkmark($file_id)
	{
		$mark = 1;
		$select = $this->_conn
                       ->select()
                       ->from(array('m' => $this->_prefix . 'multimedia_file_set_assoc'), array('num_image_mark' => 'COUNT(*)'))
                        
                       ->where("m.file_id = " . $file_id . " and m.mark = " . $mark);
                        
		//echo $select;
        return $select->query()->fetch()->num_image_mark;
	}
}